//
//  FRPGalleyViewController.m
//  FunctionalReactivePixels
//
//  Created by Ran on 15/9/2.
//  Copyright (c) 2015年 gintong. All rights reserved.
//

#import "FRPGalleyViewController.h"
#import "FRPGalleyFlowLayout.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <ReactiveCocoa/RACEXTScope.h>
#import <ReactiveCocoa/RACDelegateProxy.h>
#import "FRPPhotoImporter.h"
#import "FRPCollectionViewCell.h"
#import "FRPFullSizePhotoViewController.h"

@interface FRPGalleyViewController ()

@property(nonatomic, strong)NSArray *photosArray;

@property(nonatomic, strong)id collectionViewDelegate;

@end

@implementation FRPGalleyViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init
{
    FRPGalleyFlowLayout *flowLayout = [[FRPGalleyFlowLayout alloc] init];
    self = [self initWithCollectionViewLayout:flowLayout];
    if (!self) {
        return nil;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"McD on sale";
    
    self.view.backgroundColor = [UIColor whiteColor];
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    // Register cell classes
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerNib:[UINib nibWithNibName:@"FRPCollectionViewCell" bundle:nil]forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
    
    
    @weakify(self);
//    [RACObserve(self, photosArray) subscribeNext:^(id x) {
//        @strongify(self);
//        [self.collectionView reloadData];
//    }];
    
    RACDelegateProxy *viewControllerDelegate = [[RACDelegateProxy alloc] initWithProtocol:@protocol(FRPFullSizePhotoViewControllerDelegate)];
    [[viewControllerDelegate rac_signalForSelector:@selector(userDidScroll:toPhotoAtIndex:) fromProtocol:@protocol(FRPFullSizePhotoViewControllerDelegate)] subscribeNext:^(RACTuple *value) {
        @strongify(self);
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:[value.second integerValue] inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
    }];
    self.collectionViewDelegate = [[RACDelegateProxy alloc] initWithProtocol:@protocol(UICollectionViewDelegate)];
    [[self.collectionViewDelegate rac_signalForSelector:@selector(collectionView:didSelectItemAtIndexPath:)] subscribeNext:^(RACTuple *value) {
        @strongify(self);

        FRPFullSizePhotoViewController *controller = [[FRPFullSizePhotoViewController alloc] initWithPhotoModels:self.photosArray currentPhotoIndex:[(NSIndexPath *)value.second item]];
        controller.delegate = (id<FRPFullSizePhotoViewControllerDelegate>)viewControllerDelegate;
        [self.navigationController pushViewController:controller animated:YES];
        
    }];
    self.collectionView.delegate = (id<UICollectionViewDelegate>)self.collectionViewDelegate;
    
//    [self loadPhotos];
    
//    RACSignal *photoSignal = [FRPPhotoImporter importPhotos];
//    RACSignal *photosLoaded = [photoSignal catch:^RACSignal *(NSError *error) {
//        NSLog(@"error occured -- %@", error);
//        return [RACSignal empty];
//    }];
//    
//    RAC(self, photosArray) = photosLoaded;
//    [photosLoaded subscribeCompleted:^{
//        @strongify(self);
//        [self.collectionView reloadData];
//    }];
    
    RAC(self, photosArray) = [[[[FRPPhotoImporter importPhotos] doCompleted:^{
        @strongify(self);
        [self.collectionView reloadData];
    }] logError] catchTo:[RACSignal empty]];
    
}

- (void)loadPhotos{
 
    [[FRPPhotoImporter importPhotos] subscribeNext:^(id x) {
        self.photosArray = x;
    } error:^(NSError *error) {
        NSLog(@"error: %@", error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.photosArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    FRPCollectionViewCell *cell = (FRPCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [cell setPhotoModel:self.photosArray[indexPath.item]];
    return cell;
}

//#pragma mark <UICollectionViewDelegate>
//
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//}



//#pragma mark - Delegate
//
//- (void)userDidScroll:(FRPFullSizePhotoViewController *)viewController toPhotoAtIndex:(NSInteger)index
//{
//    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
//}

@end
